package com.example.testandroid.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;

import com.example.testandroid.R;

public class ClockActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_clock);
		//计时器
		final Chronometer ch = (Chronometer)findViewById(R.id.chronometer);
		Button start = (Button)findViewById(R.id.clockStart);
		start.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ch.setBase(SystemClock.elapsedRealtime());
				ch.start();
			}
		});
		
		ch.setOnChronometerTickListener(new OnChronometerTickListener() {
			
			@Override
			public void onChronometerTick(Chronometer chronometer) {
				if(SystemClock.elapsedRealtime() - ch.getBase() > 20 * 1000){
					ch.stop();
				}
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.clock, menu);
		return true;
	}

}
